<template>
	<view class="analyse">
		<view class="header">
			<view class="form">
				<view class="select">
					<view class="text"> <text>*</text>筛选月份: </view>
					<view class="input">
						<input style="width: 100%;" @tap="showMonth = true" :value="month" disabled type="text"
							placeholder="请选择月份" />
					</view>
				</view>
				<view class="select">
					<view class="text"> <text>*</text>筛选日期: </view>
					<view class="input">
						<input @tap="openDatePick('beginTime')" :placeholder="'请选择'" :value="formData.beginTime"
							disabled type="text" />
						<text>到</text>
						<input @tap="openDatePick('endTime')" :placeholder="'请选择'" :value="formData.endTime" disabled
							type="text" />
					</view>
					<view class="total">
						总金额:<text>{{(totalPrice*1000-totalOrderReturnAmt*1000)/1000 | unifyNumber}}</text> </view>
				</view>
			</view>
		</view>
		<view class="download_area">
			<view class="title">
				<h3>对账单下载地址</h3>
			</view>
			<view v-if="!interlinkage" class="download"> excel下载链接…… </view>
			<view v-else class="download"> {{interlinkage}} </view>
		</view>
		<view class="btn">
			<view @tap="createInterlinkage" class=""> 生成链接 </view>
			<view @tap="downloadFile(interlinkage)" class=""> 保存文件 </view>
			<view @click="share(interlinkage)" class=""> 微信分享 </view>
		</view>
		<view class="download_area">
			<view class="title">
				<h3>对账单凭证下载地址</h3>
			</view>
			<view v-if="!detailZip" class="download"> zip下载链接…… </view>
			<view v-else class="download"> {{detailZip}} </view>
		</view>
		<view class="btn">
			<view @tap="createDetailZip" class=""> 生成链接 </view>
			<view @tap="downloadFile(detailZip)" class=""> 保存文件 </view>
			<view @click="share(detailZip)" class=""> 微信分享 </view>
		</view>
		<myDatePick mode="date" @bindDateChange="bindDateChange" ref="myDatePick" :show.sync="showDate" />
		<u-picker :itemHeight="80" @confirm="monthConfirm" @cancel="showMonth = false" v-model="month" :show="showMonth"
			:columns="columns"></u-picker>
	</view>
</template>

<script>
	// #ifndef MP
	const FileShare = uni.requireNativePlugin('life-FileShare');
	// #endif
	import myDatePick from "../../components/myDatePick.vue";
	export default {
		components: {
			myDatePick
		},
		data() {
			return {
				showMonth: false,
				columns: [
					['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12']
				],
				month: "",
				formData: {
					cid: "",
					beginTime: '',
					endTime: '',
				},
				showDate: false,
				interlinkage: "",
				detailZip: "",
				totalPrice: 0,
				totalOrderReturnAmt: 0,
				topName: ""
			};
		},
		onLoad: async function(options) {
			wx.setNavigationBarTitle({
				title: options.topName
			});
			this.topName = options.topName
			this.formData.cid = options.cid
			// this.getList(this.formData)
			// await this.getOrderSaleTotalValue()
			// this.formData.beginTime = uni.$u.timeFormat(Date.now(),'yyyy-mm-dd')
			// console.log(Date.now());
			// this.formData.endTime = uni.$u.timeFormat(Date.now(),'yyyy-mm-dd')

		},
		methods: {
			async getOrderSaleTotalValue() {
				let requestData = uni.$u.deepClone(this.formData);
				if (requestData.endTime) requestData.endTime = `${requestData.endTime} 23:59:59`;
				if (requestData.beginTime) requestData.beginTime = `${requestData.beginTime} 00:00:00`;
				Object.keys(requestData).map((item) => {
					if (requestData[item] == '' || requestData[item] == null) {
						delete requestData[item]
					}
				});
				let total = await this.$api.orderSaleTotalValueApi({
					data: requestData
				})
				this.totalPrice = total.result.totalOrderAmt
				this.totalOrderReturnAmt = total.result.totalOrderReturnAmt
			},
			monthConfirm(e) {
				const date = new Date()
				this.month = e.value[0] + "月"
				let date1 = new Date(date.getFullYear(), +e.value[0] - 1, 1)
				let date2 = new Date(date.getFullYear(), +e.value[0], 0)
				this.formData.beginTime = uni.$u.timeFormat(date1);
				this.formData.endTime = uni.$u.timeFormat(date2);
				console.log(Number(date1));
				this.showMonth = false
				this.getList(this.formData)
				this.createDetailZip()
				this.getOrderSaleTotalValue()
			},
			share(url) {
				if (!url) {
					uni.showToast({
						title: "请生成链接",
						icon: "none"
					})
				}
				// #ifdef APP-PLUS
				var dtask = plus.downloader.createDownload(url, {
					filename: "_downloads/download/"
				}, function(d, status) {
					// 下载完成
					if (status == 200) {
						FileShare.render({
							type: "WX", //QQ为QQ，微信为WX，系统默认是SYSTEM，不填写默认SYSTEM
							filePath: plus.io.convertLocalFileSystemURL(d.filename),
						}, result => {
							console.log(result);
						});
					} else {
						console.log("Download failed: " + status);
					}
				});
				dtask.start();
				return
				// #endif
				// #ifdef MP-WEIXIN
				url=url.replace("http:","https:")
				wx.downloadFile({
					url, //可以是后台传过来的路径
					success: function(res) {
						const filePath = res.tempFilePath
						console.log(filePath);
						wx.shareFileMessage({
							filePath: filePath,
							success: function(res) {
								//成功
								
							}
						})
					}
				})
				return
				// #endif
				uni.showToast({
					title: "请在app分享",
					icon: "none"
				})
			},
			async createDetailZip() {
				// uni.showLoading({
				// 	title: "加载中...",
				// });
				let requestData = uni.$u.deepClone(this.formData);
				requestData.endTime = `${requestData.endTime} 23:59:59`;
				requestData.beginTime = `${requestData.beginTime} 00:00:00`;
				Object.keys(requestData).map((item) => {
					if (requestData[item] == '' || requestData[item] == null) {
						delete requestData[item]
					}

				});
				try {
					let data = await this.$api.detailZipApi(requestData)
					this.detailZip = data.result
				} catch (e) {
					//TODO handle the exception
					this.detailZip = ""
				}
				// uni.hideLoading();
			},
			downloadFile(urlStr) {
				if (!urlStr) {
					uni.showToast({
						title: "请生成链接",
						icon: "none"
					})
					return
				}
				var urlStr = encodeURI(urlStr)
				// #ifdef APP-PLUS
				plus.runtime.openURL(urlStr)
				return
				// #endif
				// #ifdef MP-WEIXIN
				urlStr=urlStr.replace("http:","https:")
				wx.downloadFile({
					url: urlStr,
					success: (res) => {
						wx.openDocument({
							filePath:res.tempFilePath || res.filePath,
							showMenu:true,
							
						})
						
					}
				})
				return
				// #endif
				window.open(urlStr)
				return


			},
			createInterlinkage() {
				this.getList(this.formData)
			},
			openDatePick(key) {
				this.showDate = true
				this.dateKey = key
			},
			bindDateChange(e) {
				this.month = ""
				let value = e
				let key = this.dateKey
				this[key] = value;
				// if (key === "endTime") {
				// 	value = `${value} 23:59:59`;
				// } else if (key === "beginTime") {
				// 	value = `${value} 00:00:00`;
				// }
				this.formData[key] = value;
				if (key === "endTime") {
					this.getList(this.formData)
					this.createDetailZip(this.formData)
					this.getOrderSaleTotalValue()
				}
			},
			async getList(formData) {
				// uni.showLoading({
				// 	title: "加载中...",
				// });
				let requestData = uni.$u.deepClone(formData);
				requestData.endTime = `${requestData.endTime} 23:59:59`;
				requestData.beginTime = `${requestData.beginTime} 00:00:00`;
				Object.keys(requestData).map((item) => {
					if (requestData[item] == '' || requestData[item] == null) {
						delete requestData[item]
					}
				});
				try {
					let data = await this.$api.customerApi(requestData)
					console.log(data);
					this.interlinkage = data.result
				} catch (e) {
					//TODO handle the exception
					this.interlinkage = ""
					this.totalPrice = ""
				}

				// uni.hideLoading();
			}
		}
	};
</script>



<style lang="scss" scoped>
	page {
		background: #f1f1f1;
	}

	.analyse {
		background: #f1f1f1;
		height: 100vh;

		.header {
			padding: 16rpx 29rpx;
			background-color: #fff;
		}

		.form {
			.select {
				text-indent: 1em;

				.text {
					margin: 20rpx 0;
					color: #bdbdbd;

					text {
						color: #fe3430;
					}
				}

				input {
					width: 692rpx;
					height: 94rpx;
					background: #f2f2f2;
					border-radius: 47rpx;
				}

				.input {
					display: flex;
					align-items: center;
					justify-content: space-between;

					input {
						width: 271rpx;
					}

					text {
						text-align: left;
					}
				}

				.total {
					padding: 20rpx 0;

					text {
						color: #fe3430;
					}
				}
			}
		}
	}

	.download_area {
		padding: 17rpx;

		.title {
			margin: 20rpx 0;
		}

		.download {
			word-wrap: break-word;
			word-break: break-all;
			width: 717rpx;
			height: 132rpx;
			background: #ffffff;
			border-radius: 20rpx;
			padding: 34rpx;
		}
	}

	.btn {
		display: flex;
		margin-top: 19rpx;
		justify-content: space-around;

		view {
			background-image: url("/static/images/Registration/button.png");
			background-size: 223rpx 109rpx;
			width: 223rpx;
			height: 109rpx;
			border-radius: 49rpx;
			color: #fff;
			text-align: center;
			line-height: 90rpx;
		}
	}
</style>